package app.stock.web;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import app.common.Util;
import app.stock.common.Account;

public class StockMainServlet extends HttpServlet {

    static String defaultServletPath = "/stock";

    @Override
    public void service(ServletRequest req, ServletResponse resp)
            throws ServletException, IOException {
        super.service(req, resp);
        // System.out.println("StockMainServlet.service");
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String page = req.getParameter(WebConstants.key_page);
        String action = req.getParameter(WebConstants.key_action);
        System.out.println("StockMainServlet.doGet:" + WebConstants.key_page
                + "=" + page + "," + WebConstants.key_action + "=" + action);
        // System.out.print("Log properties : [");
        // System.out.print(", getPathTranslated()=" + req.getPathTranslated());
        // System.out.print(", getHeader(Referer)=" + req.getHeader("Referer"));
        // System.out.println(" ]");
        if (Util.hasValue(action) && "logout".equals(action)) {
            req.getSession().removeAttribute(
                    WebConstants.sessionAttrName_Account);
            resp.sendRedirect(WebConstants.page_login);
            return;
        } else if (req.getSession().getAttribute(
                WebConstants.sessionAttrName_Account) == null) {
            handleLogin(req, resp);
            // resp.sendRedirect("StockCtrl?page=login.jsp");
            // return;
        }
        if (req.getSession().getAttribute(WebConstants.sessionAttrName_Account) != null) {
            RequestHandler rh = new RequestHandler();
            rh.handleRequest(req, resp);
        }
    }

    private void handleLogin(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String remoteUser = req.getRemoteUser();
        Principal user = req.getUserPrincipal();
        Account acct = new Account();
        if (remoteUser != null) {
            acct.name = remoteUser;
        } else if (user != null) {
            acct.name = user.getName();
        }
        req.getSession().setAttribute(WebConstants.sessionAttrName_Account,
                acct);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // System.out.println("StockMainServlet.doPost");
        this.doGet(req, resp);
    }

    @Override
    public void destroy() {
        System.out.println("StockMainServlet.destroy");
    }
}
